package top.cluski.feieprint.framework.validate;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ValidateAspect {
    @Pointcut("within(@org.springframework.web.bind.annotation.RestController *)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object validate(ProceedingJoinPoint joinPoint) throws Throwable {
        for (Object arg : joinPoint.getArgs()) {
            if (arg instanceof Validatable validatable) {
                validatable.validate();
            }
        }
        return joinPoint.proceed();
    }
}
